#===============================================================================
# Koro Business Environment
# Copyright (C) 2006,2007  Antonio Corroppoli
#
# This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

#validators per i campi
import string
import wx
import koro.ui.wxgui.dialogs as dialogs

mREAD_ONLY = 2
mREQUIRED  = 4
mALPHA_ONLY = 8
mDIGIT_ONLY = 16

class mValidator(wx.PyValidator):
    def __init__(self,style):
        """ Standard constructor.
        """
        wx.PyValidator.__init__(self)
        print "costruttore"
        self.style=style
        self.Bind(wx.EVT_CHAR,self.OnChar)

##    def OnChar(self,evt):
##        print 'OnChar'
##        r= self.Validate(evt.GetEventObject())
##        print r
##        r and evt.Skip(True)
##        return
    def OnChar(self, evt):
        print 'onchar'
        key = evt.GetKeyCode()
        style=self.style
        tc=evt.GetEventObject()

        if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
            evt.Skip()
            tc.SetBackgroundColour(
                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
            tc.Refresh()
            return

        if style & mALPHA_ONLY == mALPHA_ONLY and chr(key) in string.letters:
            tc.SetBackgroundColour(
                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
            tc.Refresh()
            evt.Skip()
            return

        if style & mDIGIT_ONLY == mDIGIT_ONLY and chr(key) in string.digits:
            tc.SetBackgroundColour(
                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
            tc.Refresh()
            evt.Skip()
            return

        if style & mALPHA_ONLY != mALPHA_ONLY and style & mDIGIT_ONLY != mDIGIT_ONLY:
            tc.SetBackgroundColour(
                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
            tc.Refresh()
            evt.Skip()
            return

        if not wx.Validator_IsSilent():
            wx.Bell()

        # Returning without calling even.Skip eats the event before it
        # gets to the text control
        return
         

    def Clone(self):
        print 'Clone'
        return mValidator(self.style)

    def Validate(self,win):
        tc = self.GetWindow()
        #print 'Validate 1',win,tc
        #tc=win
        val = tc.GetValue()

        style = self.style

##        if style & mALPHA_ONLY == mALPHA_ONLY:
##            print 'solo lettere'
##            for x in val:
##                if x not in string.letters:
##                    return False
##        if style & mDIGIT_ONLY == mDIGIT_ONLY:
##            print 'solo numeri',val
##            for x in val:
##                if x not in string.digits:
##                    return False
        if style & mREQUIRED == mREQUIRED:
            print 'richiesto'
            if len(val) == 0:
                print 'validator fail'
                #msgbox = dialogs.mDrawnMsgDialog(win,dialogs.mMSG_REQUIRED,tc.GetScreenPositionTuple())
                msgbox = dialogs.mPopupMsg(win,dialogs.mMSG_REQUIRED,tc.GetScreenPositionTuple())
                
                #msgbox.Destroy()
                tc.SetBackgroundColour((205,224,254))
                tc.SetFocus()
                tc.Refresh()
                msgbox.Show()        
                return False
            else:
                tc.SetBackgroundColour(
                    wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
                tc.Refresh()
                return True

    def TransferToWindow(self):
        """ Transfer data from validator to window.

             The default implementation returns False, indicating that an error
             occurred.  We simply return True, as we don't do any data transfer.
        """
        return True # Prevent wxDialog from complaining.


    def TransferFromWindow(self):
        """ Transfer data from window to validator.

             The default implementation returns False, indicating that an error
             occurred.  We simply return True, as we don't do any data transfer.
        """
        return True # Prevent wxDialog from complaining.
        


